package com.alipay.freeblog.service.impl;

import com.alipay.freeblog.dal.daoobject.TemplateDO;
import com.alipay.freeblog.dal.interfaces.TemplateDao;
import com.alipay.freeblog.domain.Template;
import com.alipay.freeblog.service.TemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Ted
 * Date: 11-9-25
 * Time: 上午2:33
 */
@Service("templateService")
public class TemplateServiceImpl implements TemplateService {

	private TemplateDao templateDao;

	@Override
	public boolean updateTemplate(String templateName, String vm) throws Exception {
		TemplateDO templateDO = new TemplateDO();
		templateDO.setName(templateName);
		templateDO.setVm(vm);
		return templateDao.updateTemplate(templateDO);
	}

	@Override
	public Template getTemlate(String templateName) throws Exception {
		TemplateDO templateDO = templateDao.getTemplateByName(templateName);
		Template template = new Template();
		BeanUtils.copyProperties(templateDO, template);
		return template;
	}

	@Override
	public List<Template> getTemplateList(String name) throws Exception {
		List<TemplateDO> doList = templateDao.getTemplateListByName(name);
		List<Template> list = new ArrayList<Template>();
		if (doList != null) {
			for (TemplateDO templateDO : doList) {
				Template template = new Template();
				BeanUtils.copyProperties(templateDO, template);
				list.add(template);
			}
		}
		return list;
	}

	public TemplateDao getTemplateDao() {
		return templateDao;
	}

	public void setTemplateDao(TemplateDao templateDao) {
		this.templateDao = templateDao;
	}
}
